__author__ = 'Muscle'


from mui import *
from mui.Widgets import *
import time
import mui.color as color

class MyWindow(MUIWindow):
    def __init__(self, **kwargs):
        MUIWindow.__init__(self, **kwargs)
        self.background = 'dialog\\volume_bg.png |(10,10,10,10)'

        self._canvas = MUICanvas(self)
        c = self._canvas
        c.bindData('height', self, 'height', dir = '<-')
        c.bindData('width', self, 'width', dir = '<-')
        c.bgColor = color.yellow
    

        o = MUIComponent(c)
        c.background = 'dialog\\volume_bg.png |(10,10,10,10)'
        o.bgColor = color.darkblue
        o.size = 200, 30

        o1 = MUIComponent(c)
        o1.bgColor = color.red
        o1.size = 100, 50

        o2 = MUIComponent(c)
        o2.bgColor = color.green
        o2.size = 100, 50

        o3 = MUIComponent(c)
        o3.bgColor = color.blue
        o3.size = 50, 50

        o4 = MUIComponent(c)
        o4.bgColor = color.purple
        o4.size = 50, 50
        #Sample Canvas dock
        c.dock(o,  {'l':50,  't':50, 'b':50, 'r':50})
        c.dock(o1, {'l':100, 't':100, 'b':100})
        c.dock(o2, {'r':100, 't':100, 'b':100})
        c.dock(o3, {'h':True, 't' :100})
        c.dock(o4, {'v':True, 'l':200, 'r':300})

        # for FPS
        self.__fpsCount = 0
        self.__fpsTimeStart = 0;
        self.__fosTimeEnd = 0;
        self.fpsText = MUIText(self, right = self.width-20, top = 20, width = 200, height = 40, text = "FPS: 0", fontSize = 20, fontColor = color.blue, fontWeight = 'Bold')
        self.bindData('width', self.fpsText, 'right', dir = '->', converter =  lambda x: x-20 )


    def on_paint(self):
        if (self.__fpsCount == 0):
            self.__fpsTimeStart = time.clock()
        super(MyWindow, self).on_paint()
        self.__fpsCount += 1

        if (self.__fpsCount >= 120):
            self.__fpsTimeEnd = time.clock()
            diffTime = self.__fpsTimeEnd - self.__fpsTimeStart
            fps = self.__fpsCount / diffTime
            self.fpsText.text = "FPS: " + str(int(fps))
            self.__fpsCount = 0



def main():
    mui.muiInit(main)
    g_DXDevice = muiDX.CDXDevice()
    w = MyWindow(width = 1024, height = 768, dxDevice = g_DXDevice)
    w.ShowWindow(1)
    muiApp.run()
    g_DXDevice.Release()
    mui.muiUnInit()



main()
  